package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Address;
import com.markerhub.mapper.AddressMapper;
import com.markerhub.service.AddressService;
import com.markerhub.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>12

 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;
    @Autowired
    private AddressMapper addressMapper;

    //@RequiresAuthentication//必须登录认证才能访问的注解
    @GetMapping("/addresslist")
    public Result alladdress(){
        //利用分页查询全部收获地址
        Page page = new Page(1,4);

        IPage pageData= addressService.page(page,new QueryWrapper<Address>());
        //IPage pageData= blogService.page(page,new QueryWrapper<Blog>().orderByDesc("created"));
        //blogService.page(page,new QueryWrapper<Blog>().orderByDesc("created"));

        return Result.succ(pageData);
    }


    @GetMapping("/{userid}")//获取地址信息
    public Result detail(@PathVariable(name="userid")   Long userid,@RequestParam(defaultValue = "1") Integer currentpage,@RequestParam(defaultValue = "1") Integer size){
        if(userid==null){
            return  Result.faile("请先登录");
        }
        Page page = new Page(currentpage,3);
        //Address blog = addressService.getById(1);
        if(size!=1){
             page = new Page(currentpage,999);//
        }else {
             page = new Page(currentpage,3);//
        }

        IPage pageData= addressService.page(page,new QueryWrapper<Address>().eq("user_id",userid));
        Assert.notNull(pageData,"无地址信息请添加地址");
        return  Result.succ(pageData);

    }


    @GetMapping("/adressone/{userid}")
    public Result addressdetailone(@PathVariable(name="userid") int userid){
        Address blog = addressService.getById(userid);
        Assert.notNull(blog,"该博客已被删除");
        return  Result.succ(blog);
    }

    @RequiresAuthentication //出bug了需要前端发送head文件才可以读取
    @GetMapping("/deleteadd/{deleteid}")
    public Result deleteaddress(@PathVariable(name="deleteid") int deleteid){
        System.out.println("deleteid"+deleteid);
        //int rows =addressMapper.deleteById(deleteid);
        Address blog = addressService.getById(deleteid);
        boolean b = addressService.removeById(deleteid);
        Assert.isTrue(blog.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(),"没有权限编辑");
        if(b){ return Result.succ("删除成功");}else {
            return Result.succ("删除失败");
        }


    }



    @RequiresAuthentication
    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody Address blog){
         Address temp=null;
        System.out.println("**************************************************");
        System.out.println(blog);
        if(blog.getId()!=null){
            temp=addressService.getById(blog.getId());
            System.out.println("有id");
            //只能编辑自己的文章
            Assert.isTrue(temp.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(),"没有权限编辑");
        }else
        {
            temp = new Address();
            temp.setUserId(ShiroUtil.getProfile().getId().intValue());

        }


        //BeanUtil.copyProperties(temp,blog,"id","userId","phone","address","realname","idcard"); //copyProperties用法错了醉了
        temp.setAddress(blog.getAddress()).setId(blog.getId()).setIdcard(blog.getIdcard()).setPhone(blog.getPhone()).setRealname(blog.getRealname());
        System.out.println("**************************************************");
        System.out.println(blog);
        System.out.println(temp);
        addressService.saveOrUpdate(temp);

        return  Result.succ(null);
    }





}
